using UnityEngine;
using System.Collections;


public class Damagable : MonoBehaviour {

	private int lifeMax = 100;
	public int life;
	public bool alive = true;
	public bool debug = false;
	public ParticleSystem emitter = null;
	private GameObject GUIHandler;
	public float minimumHeight = 20;
	public bool persistant = false;
	public bool isSnake = false;
	
	private RespawnHandler respawnHandler;
	
	//public var respawnTime : int = 3;
	// Use this for initialization
	void Start () { 
		GUIHandler = GameObject.Find("GUIHandler");
		
		if(gameObject.GetComponent<RigidbodyFPSController>() != null){
			lifeMax = gameObject.GetComponent<RigidbodyFPSController>().GetHP();
			if(debug)Debug.Log("Fetched that shit like a muthafucka");
		}
		else{
			if(debug)Debug.Log (gameObject + " isn't a player, right? Well then, carry on...");
		}
		life = lifeMax;
		
		if (GUIHandler.GetComponent<GUIHealthBar>() != null && GUIHandler != null){
			GUIHandler.GetComponent<GUIHealthBar>().setMaxHealth(lifeMax);
			GUIHandler.GetComponent<GUIHealthBar>().setHealth(lifeMax);
		}
	}
	
	public int getMaxLife(){
		return lifeMax;
	}
	
	// Update is called once per frame
	void Update () {	
		if(isSnake){
			if (life <= 0) {
				alive = false;
			}
		}else{
			if (!alive) {
				if (persistant) {
					//TODO: Check if working?!?
					if (respawnHandler.spawnObject(gameObject)){
						ChangeHP(lifeMax);
						alive = true;
						//GUIHandler.GetComponent<PlayerGUI>().setHealth(lifeMax);
					}else
					Destroy(gameObject);
				}
			}
				
			if (life <= 0) {
				alive = false;
			}
			
			if (this.transform.position.y < minimumHeight) {
				alive = false;
			}
		}
	}
	
	//[RPC]
	[RPC]
	public void DamageRPC(int amount, Vector3 direction)
	{
		
		Debug.Log("BLAAAA: "+enabled);
		if(this.enabled){
			Debug.Log("borde ljuda");
			Listeners.inst().notify(Channel.ON_FIGHT,gameObject.GetComponent<PlayerState>());
		}
	
		life = life - amount;
		if (debug) Debug.Log (gameObject + " took " + amount + " damage.");
		
		if (emitter != null) {
			for(int i = 0; i < 5; i++){
				emitter.Emit (3);
			}
		}
		//Show Damage Indication
		if (GUIHandler != null){
			if (GUIHandler.GetComponent<HitGUI>() != null){
				Vector3 cameraPosition 	= Camera.main.transform.position;
				Vector3 cameraDirection = Camera.main.transform.forward;
				GUIHandler.GetComponent<HitGUI>().displayHit(direction,cameraPosition,cameraDirection);
			}
			if (GUIHandler.GetComponent<GUIHealthBar>() != null)
				if(gameObject.GetComponent<WeaponSystem>().enabled)
					GUIHandler.GetComponent<GUIHealthBar>().setHealth(life);
		}
		
		if (GetComponent<Entity>() == null) {
			Listeners.inst().notify(Channel.ON_DAMAGE, GetComponent<PlayerState>());
		} else {
			Listeners.inst().notify(Channel.ON_DAMAGE, GetComponent<Entity>().getOwner().GetComponent<PlayerState>());
		}
		
	}
	
	[RPC]
	public void MeleeDamageRPC(int amount, Vector3 direction, int scorer)
	{
	
		life = life - amount;
		if (debug) Debug.Log (gameObject + " took " + amount + " damage.");
		
		if(life<0 && Network.isServer){
			ScoreListener.inst().notify(new ScoreEvent(scorer,ScoreAction.UPDATE,1));
		}
		
		if (emitter != null) {
			for(int i = 0; i < 5; i++){
				emitter.Emit (3);
			}
		}
		//Show Damage Indication
		if (GUIHandler != null){
			if (GUIHandler.GetComponent<HitGUI>() != null){
				Vector3 cameraPosition 	= Camera.main.transform.position;
				Vector3 cameraDirection = Camera.main.transform.forward;
				GUIHandler.GetComponent<HitGUI>().displayHit(direction,cameraPosition,cameraDirection);
			}
			if (GUIHandler.GetComponent<GUIHealthBar>() != null)
				if(gameObject.GetComponent<WeaponSystem>().enabled)
					GUIHandler.GetComponent<GUIHealthBar>().setHealth(life);
		}
		
		if (GetComponent<Entity>() == null) {
			Listeners.inst().notify(Channel.ON_DAMAGE, GetComponent<PlayerState>());
		} else {
			Listeners.inst().notify(Channel.ON_DAMAGE, GetComponent<Entity>().getOwner().GetComponent<PlayerState>());
		}
		if(this.enabled){
			Listeners.inst().notify(Channel.ON_FIGHT,gameObject.GetComponent<PlayerState>());
		}
	}

	public void Damage(int amount, Vector3 point, Vector3 direction)
	{

	life = life - amount;
	if (life > lifeMax)
			life = lifeMax;
	if(debug) Debug.Log (gameObject + " took " + amount + " damage.");


		if(emitter != null){
			for(int i = 0; i < 5; i++){
				emitter.Emit (point, -direction * i * 10, 0.1f, 4.0f, Color.red);
			}
		}


	}

		

	
	public void ChangeMaxHP(int currentHP){
		lifeMax = currentHP;
		if (GUIHandler != null){
			if (GUIHandler.GetComponent<GUIHealthBar>() != null){
				GUIHandler.GetComponent<GUIHealthBar>().setMaxHealth(lifeMax);
			}
		}
	}
	

	public void ChangeHP(int newHP) {
		if (GUIHandler == null)
			GUIHandler = GameObject.Find("GUIHandler");		
		life = newHP;
		if (GUIHandler.GetComponent<GUIHealthBar>() != null && GUIHandler != null){
			GUIHandler.GetComponent<GUIHealthBar>().setHealth(newHP);
		}
		else
		Debug.LogWarning("Could not find GUIHandler object or GUIHealthBar script on it.");
	}
	
	

	public void setRespawnHandler(RespawnHandler handler) {
		this.respawnHandler = handler;
	}
	
}